<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Project_content extends CI_Controller{
	public function __construct() {
		parent::__construct ();
		$this->load->model("project_content_model");
		$this->load->model("user_model");
		$this->load->model("project_model");
		$this->load->model("project_content_comment_model");
		$this->load->model('category_model');
		$whereArr = array("status"=>1);
		$this->category_list = $this->category_model->resultAll($whereArr);
	}
	
	public function index($pid=0,$page=1){
		$project_message = $this->find_project_message($pid);
		$username = $this->find_user_name($project_message['uid']);
		$whereArr = array("status"=>1,"pid"=>$pid);
		$count = $this->project_content_model->count ( $whereArr );
		$params = array(
				"url_template" => site_url ( "Project_content/index/{$pid}/{page}" ),
				"page_count" => $count,
				"page_now" => $page,
				"page_size" => 4,
				"class"=>array("ul"=>"","disabled"=>"am-disabled","active"=>"am-active")
		);
		$params["total_pages"] = ceil ( $params['page_count'] / $params['page_size'] );
		$this->load->library ( 'paging', $params );
		$project_content_list = $this->project_content_model->result($whereArr,$page,4,"sort@DESC");
		
		foreach($project_content_list as $key =>$value){
			$project_content_list[$key]['comment'] = $this->find_content_comment($value['pcid']);
		}
		$data['project_title'] = $project_message['title'];
		$data['pid'] = $project_message['pid'];
		$data['project_content_list'] = $project_content_list;
		$data['username'] = $username;
		$data['category_list'] = $this->category_list;
		$this->load->view("project_content_list",$data);
	}
	/*
	 *单条内容记录
	 */
	public function project_content_detail($pcid=0,$page=1){
		
		$whereArr = array("pcid"=>$pcid,'status'=>1);
		$project_content_message = $this->project_content_model->row($whereArr);
		
		$whereArr = array("pcid"=>$pcid,"status"=>1);
		$count = $this->project_content_comment_model->count($whereArr);
		$params = array(
				"url_template" => site_url ( "Project_content/project_content_detail/{$pcid}/{page}" ),
				"page_count" => $count,
				"page_now" => $page,
				"page_size" => 10,
				"class"=>array("ul"=>"","disabled"=>"am-disabled","active"=>"am-active")
		);
		$params["total_pages"] = ceil ( $params['page_count'] / $params['page_size'] );
		$this->load->library ( 'paging', $params );
		$project_content_comment_list = $this->project_content_comment_model->result($whereArr,$page,10,"createtime@DESC");
		foreach($project_content_comment_list as $key => $value){
			$project_content_comment_list[$key]['logo'] = $this->find_user_logo($value['uid']);
			$project_content_comment_list[$key]['username'] = $this->find_user_name($value['uid']);
		}
		$data['project_content_comment_list'] = $project_content_comment_list;
		$data['project_content_message'] = $project_content_message;
		$data['category_list'] = $this->category_list;
		$this->load->view("project_content_detail",$data);
	}
	/*
	 *添加文本内容 
	 */
	public function addText(){
		$pid = $this->input->post('pid')+0;
		$content = $this->input->post('content');
		if(empty($content)){
			$dataArr['message']="填写的内容文本不能为空!";
			$this->load->view("assists/error",$dataArr);
		}else{
			$data['type'] = 0;
			$data['content'] = $content;
			$count = $this->find_project_content_count($pid);
			$data['pid'] = $pid;
			$data['sort'] = $count+1;
			$result = $this->project_content_model->insert($data);
			if($result){
				$dataArr['message'] = "添加内容文本成功!!";
				$this->load->view("assists/success",$dataArr);
			}else{
				$dataArr["message"] = "添加内容文本失败!!";
				$this->load->view("assists/error",$dataArr);
			}
		}
	}
	/*
	 *升序内容的顺序 
	 */
	public function up_sort($pcid=1,$sort=1){
		/*$fp = fopen("1.txt", "w+");
		fputs($fp, $sort);
		fclose($fp);*/
		$upWhereArr = array("sort >"=>$sort,"status"=>1,"check"=>3);
		$updata = $this->project_content_model->row_order($upWhereArr,"sort@ASC");
		if(empty($updata)){
			$message['flag'] = -1;
			$message['content'] ="该内容已经在第一个位置了，不能再上去了";
			$this->show_message($message);
		}else{
			$whereArr = array("pcid" => $pcid);
			$nowData = $this->project_content_model->row($whereArr);
			if($nowData){
				$whereArr1 = array("pcid"=>$pcid);
				$dataArr1 = array("sort"=>$updata['sort']);
				$result1 = $this->project_content_model->update($dataArr1,$whereArr1);
				$whereArr2 = array("pcid"=>$updata['pcid']);
				$dataArr2 = array("sort"=>$nowData['sort']);
				$result2 = $this->project_content_model->update($dataArr2,$whereArr2);
				if($result1&&$result2){
					$message['flag'] = 1;
					$message['content'] = "升序成功!!";
					$this->show_message($message);
				}else{
					$message['flag'] = -1;
					$message['content'] = "升序失败!!";
					$this->show_message($message);
				}
			}else{
				$message['flag'] = -1;
				$message['content'] = "升序失败!!";
				$this->show_message($message);
			}
		}
	}
	/*
	 *降序内容的顺序 
	 */
	public function down_sort($pcid=1,$sort=1){
		/*$fp = fopen("1.txt", "w+");
		fputs($fp, $sort);
		fclose($fp);*/
		$downWhereArr = array("sort <"=>$sort,"status"=>1,"check"=>3);
		$downdata = $this->project_content_model->row_order($downWhereArr,"sort@DESC");
		if(empty($downdata)){
			$message['flag'] = -1;
			$message['content'] = "该内容已经在最后一个了，不能再下去了";
			$this->show_message($message);
		}else{
			$whereArr = array("pcid" => $pcid);
			$nowData = $this->project_content_model->row($whereArr);
			if($nowData){
				$whereArr1 = array("pcid"=>$pcid);
				$dataArr1 = array("sort"=>$downdata['sort']);
				$result1 = $this->project_content_model->update($dataArr1,$whereArr1);
				$whereArr2 = array("pcid"=>$downdata['pcid']);
				$dataArr2 = array("sort"=>$nowData['sort']);
				$result2 = $this->project_content_model->update($dataArr2,$whereArr2);
				if($result1&&$result2){
					$message['flag'] = 1;
					$message['content'] = "降序成功!!";
					$this->show_message($message);
				}else{
					$message['flag'] = -1;
					$message["content"] = "降序失败!!";
				}
			}else{
				$message['flag'] = "-1";
				$message['content'] = "降序失败";
				$this->show_message($message);
			}
		}
	}
	/*
	 *查询该项目下的内容的条数 
	 */
	public function find_project_content_count($pid){
		$whereArr = array("pid"=>$pid);
		$count = $this->project_content_model->count($whereArr);
		if($count){
			return $count;
		}
	}
	/*
	 *查找该项目信息
	 */
	public function find_project_message($pid){
		$whereArr = array("pid"=>$pid);
		$project = $this->project_model->row($whereArr);
		if($project){
			return $project;
		}else{
			echo "该项目已经不存在了!!";
		}
	}
	/*
	 *查找每个项目的内容的评论 
	 */
	public function find_content_comment($pcid){
		$comment_show_message = array();
		$whereArr = array("pcid"=>$pcid,"status"=>1);
		$project_content_comment_list = $this->project_content_comment_model->resultAll($whereArr);
		foreach($project_content_comment_list as $key =>$value){
			$comment_show_message[$key]['content'] = $value['content'];
			$comment_show_message[$key]['user_logo'] = $this->find_user_logo($value['uid']);
			$comment_show_message[$key]['user_id'] = $this->find_user_id($value['uid']);
		}
		return $comment_show_message;
	}
	/*
	 *查找用户头像信息
	 */
	public function find_user_logo($uid){
		$whereArr = array('uid'=>$uid);
		$user_message = $this->user_model->row($whereArr);
		return $user_message['logo'];
	}
	/*
	 *查找用户id 
	 */
	public function find_user_id($uid){
		$whereArr = array('uid'=>$uid);
		$user_message = $this->user_model->row($whereArr);
		return $user_message['uid'];
	}
	public function find_user_name($uid){
		$whereArr = array("uid"=>$uid);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user['username'];
		}
	}
	/*
	 *显示信息 
	 */
	public function show_message($message){
		echo json_encode($message);
		exit;
	}
	/*
	 *根据用户名找用户id 
	 */
	public function change_user_id($username){
		$whereArr = array("username"=>$username);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user['uid'];
		}
	}
	/*
	 *美图秀秀图片上传
	 */
	public function uploadImage($pid=1){
		if (!$_FILES['Filedata']) {
			die ( 'Image data not detected!' );
		}
		if ($_FILES['Filedata']['error'] > 0) {
			switch ($_FILES ['Filedata'] ['error']) {
				case 1 :
					$error_log = 'The file is bigger than this PHP installation allows';
					break;
				case 2 :
					$error_log = 'The file is bigger than this form allows';
					break;
				case 3 :
					$error_log = 'Only part of the file was uploaded';
					break;
				case 4 :
					$error_log = 'No file was uploaded';
					break;
				default :
					break;
			}
			die ( 'upload error:' . $error_log );
		} else {
			$img_data = $_FILES['Filedata']['tmp_name'];
			$size = getimagesize($img_data);
			$file_type = $size['mime'];
			if (!in_array($file_type, array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'))) {
				$error_log = 'only allow jpg,png,gif';
				die ( 'upload error:' . $error_log );
			}
			switch($file_type) {
				case 'image/jpg' :
				case 'image/jpeg' :
				case 'image/pjpeg' :
					$extension = 'jpg';
					break;
				case 'image/png' :
					$extension = 'png';
					break;
				case 'image/gif' :
					$extension = 'gif';
					break;
			}
		}
		if (!is_file($img_data)) {
			die ( 'Image upload error!' );
		}
		$uploads_path="/data/uploads/images";
		//图片保存路径,默认保存在该代码所在目录(可根据实际需求修改保存路径)
		$save_path =$_SERVER['DOCUMENT_ROOT']."/zcbs".$uploads_path;
		//$save = base_url('/data/uploads/images');
		$uinqid = uniqid();
		$filename = $save_path . '/' . $uinqid . '.' . $extension;
		$result = move_uploaded_file( $img_data, $filename );
		if ( ! $result || ! is_file( $filename ) ) {
			die ( 'Image upload error!' );
		}
		echo 'Image data save successed,file:' . $filename;
		$data['pid']=$pid;
		$data['type']=1;
		$data['content'] = $uploads_path.'/'.$uinqid . '.' . $extension;
		$count = $this->find_project_content_count($pid);
		$data['sort'] = $count+1;
		$result = $this->project_content_model->insert($data);
		if($result){
			echo json_encode("添加成功");
		}
		exit ();
	}
}